﻿Type=Activity
Version=3.15
@EndOfDesignText@
#Region  Activity Attributes 
	#FullScreen: False
	#IncludeTitle: True
#End Region

Sub Process_Globals
	'These global variables will be declared once when the application starts.
	'These variables can be accessed from all modules.

	Dim HttpClientTool As HttpClient
End Sub

Sub Globals
	'These global variables will be redeclared each time the activity is created.
	'These variables can only be accessed from this module.

	Dim pnlMain As Panel
	Dim btnOK As Button
	Dim edtPassword As EditText
	Dim edtUser As EditText
	Dim lblVer As Label
	Dim lblTitle As Label
	
	Dim FSql As String
	Dim FUserCode,FUserPass,FUserName As String
	Dim FDBPass As Boolean
	Dim FOldSession As String
	Dim FBackTimes As Int
	
	Dim HttpJobTool As HttpJob
	
	FBackTimes = 0
	
	
	
End Sub

Sub Activity_Create(FirstTime As Boolean)
	Activity.LoadLayout("FormLogin")
	Activity.Title = "登陆"
	lblVer.Text = "版本:" & PublicVar.GV_Version 
	
	InitLayOut
	
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub InitLayOut
	'pnlMain.Color = Activity.
	
	lblTitle.TextSize = 30
	'edtUser.Height = PublicVar.GV_ListView_ItemHeight
	edtUser.TextSize = PublicVar.GV_ListView_TextSize2

	'edtPassword.Height = edtUser.Height
	edtPassword.TextSize = edtUser.TextSize
	
	'btnOK.Height = edtUser.Height
	btnOK.TextSize = edtUser.TextSize


	Activity.AddMenuItem("选项","MenuItemLoginConfig")

End Sub

Sub MenuItemLoginConfig_Click
	If Sender = "选项" Then
		StartActivity(LoginConfig)

	End If
	
End Sub	

Sub btnOK_Click
	Dim sUserID As String
	Dim sPass As String
	sUserID = edtUser.Text.Trim
	sPass = edtPassword.Text.Trim
	
	If sUserID ="" Then	
		ToastMessageShow("请输入工号!", False)	
		edtUser.RequestFocus
		Return
	End If
	
	FUserCode = sUserID
	FUserPass = sPass
	
	'登录远程服务器 
	UserLogin
	
	'暂时不用登陆则
	'AfterLogin
	
	PublicVar.GV_UserCode = FUserCode
	
End Sub

Sub UserLogin As Boolean
	Dim sURL,sUser,sPass,sText As String
	
	FOldSession = PublicVar.GV_UserSession
	
	PublicVar.GV_UserSession = ""
	
	sUser = PublicVar.GV_DesTool.Base64encrypt(FUserCode)
	If FDBPass = False Then
		FUserPass = PublicVar.GV_DesTool.Base64encrypt(FUserPass)
	End If	
	sPass = FUserPass
	
	sText = "User=" & sUser & "&PassWord=" & sPass & "&AppType=" & PublicVar.GV_AppType
	 
	sURL = PublicVar.GetServerUrl&"/login"

	HttpJobTool.Initialize("Job1",Me)
	HttpJobTool.PostString(sURL,sText)

	ProgressDialogShow("正在登录，请稍候...")	

	Return True
End Sub

Sub JobDone (Job As HttpJob)
   Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
   If Job.Success = True Then
      If Job.JobName = "Job1" Then
				LoginSuccess(Job)
			Else If Job.JobName = "Job2" Then
				UpdateAppSuccess(Job)
      End If
   Else
		HttpJobError(Job)
   End If
   Job.Release

End Sub


Sub LoginSuccess(Job As HttpJob)   
	Dim sResult,sSession,sUserName,sCode,sData,sVersion As String
	Dim fVersion As Double
	Dim mapResult As Map
	
	ProgressDialogHide


		sResult = Job.GetString.Trim
		Log(sResult)
		'获取Session
		PublicVar.GV_JSonTool.Initialize(sResult)
		mapResult = PublicVar.GV_JSonTool.NextObject
		
		sCode = mapResult.Get("Code")
		If sCode<>"0" Then
			sResult = mapResult.Get("Message")
			ToastMessageShow("登录失败"&CRLF &sResult, True)	
			Return
		End If
		
		sData = mapResult.Get("Data")
		sData = PublicVar.GV_DesTool.Base64decrypt(sData)
		PublicVar.GV_JSonTool.Initialize(sData)
		mapResult = PublicVar.GV_JSonTool.NextObject
		
		sSession = mapResult.Get("Session")
		
		If sSession="" Then
			ToastMessageShow("获取Session失败", True)
			PublicVar.GV_UserSession = FOldSession
			Return
		End If
		PublicVar.GV_UserSession = sSession
		
		sVersion = mapResult.Get("Ver")
		If sVersion = "" Then
			ToastMessageShow("获取版本号失败", True)
			Return		
		End If
		fVersion = sVersion
		If fVersion>PublicVar.GV_Version Then
			Dim iRe As Int
			iRe = Msgbox2("当前版本号("&PublicVar.GV_Version&")太低,需要升级！"&CRLF & "要求的客户端App版本必须大于或等于"&sVersion,"提示","确定","取消","",Null)
			If iRe = DialogResponse.POSITIVE Then			
				UpdateApp
			End If
			Return			
		End If
		
		sUserName = mapResult.Get("UserName")
		FUserName = sUserName
		
		PublicVar.GV_Login = True
		
		AfterLogin
		ToastMessageShow (sUserName&",欢迎您！", True)
		
		Log("登录成功")
	
End Sub

Sub UpdateAppSuccess(Job As HttpJob)
	ProgressDialogHide
	'Job.Download(PublicVar.GV_AppPath&"/Cache/HisCXXT.apk")

	'		Response.GetAsynchronously("AppDownLoad", _ 
	'		File.OpenOutput(PublicVar.GV_AppPath&"/Cache","HisCXXT.apk",False),True,TaskId)
End Sub

Sub HttpJobError(Job As HttpJob)   
	Dim sMsg As String
	ProgressDialogHide
	
	PublicVar.GV_UserSession = FOldSession
	
	Select Job.JobName 
		Case "Job1"
			sMsg = "登录失败."
	End Select
	
	Log("Error: " & Job.ErrorMessage)
	sMsg = sMsg& CRLF & Job.ErrorMessage 
  ToastMessageShow(sMsg, True)
End Sub

Sub AfterLogin
	'StartActivity("MainXT")  '主菜单界面
	
	TZService.V_RYBM = FUserCode
	StartService(TZService)  '启动通知
	
	LoginKS.V_RYBM = FUserCode
	LoginKS.V_RYMC = FUserName
	StartActivity("LoginKS")  '登陆科室
End Sub

Sub UpdateApp
	Dim sURL,sText As String
	Dim hrRequest As HttpRequest
	
	sText = ""
	sURL = PublicVar.GetServerUrl&"/updateapp"

'	HttpJobTool.Initialize("Job2",Me)
'	HttpJobTool.PostString(sURL,sText)

	hrRequest.InitializePost2(sURL,sText.GetBytes("UTF8"))
	hrRequest.Timeout = PublicVar.GV_ConnTimeOut
	
	HttpClientTool.Initialize("HttpClientTool")
	If HttpClientTool.Execute(hrRequest, 999999) = False Then  Return

	ProgressDialogShow("正在下载，请稍候...")	

End Sub

Sub HttpClientTool_ResponseSuccess(Response As HttpResponse, TaskId As Int)
	If TaskId = 999999 Then
			ProgressDialogHide
			Response.GetAsynchronously("AppDownLoad", _ 
			File.OpenOutput(PublicVar.GV_AppPath&"/Cache","HisCXXT.apk",False),True,TaskId)
	End If
End Sub

Sub HttpClientTool_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)   
	Dim sMsg As String
	
	If TaskId = 999999 Then 
		ProgressDialogHide
	  sMsg = "登录失败."    
	  If Reason <> Null Then sMsg = sMsg & CRLF & Reason    
	  ToastMessageShow (sMsg, True)	
	End If
End Sub

Sub AppDownLoad_StreamFinish(Success As Boolean,TaskId As Int )
	If Success = False Then
		Msgbox(LastException.Message,"错误")
		Return 
	End If
	
	'运行安装apk
	Dim sFileName As String
	sFileName = "file://"&PublicVar.GV_AppPath&"/Cache/HisCXXT.apk"
	If File.Exists(PublicVar.GV_AppPath&"/Cache/","HisCXXT.apk")=True Then
	 Dim iIntent As Intent 
		iIntent.Initialize(iIntent.ACTION_VIEW, sFileName)
		iIntent.SetType("application/vnd.android.package-archive")
		StartActivity(iIntent)
	End If
	
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
	If KeyCodes.KEYCODE_BACK = KeyCode Then
		If FBackTimes =0 Then
			FBackTimes = 1
			ToastMessageShow ("再按一次退出", True)	
			Return True
		Else
			ProgressDialogHide
			FBackTimes = 0
			Return False
		End If
	Else		
		Return False
	End If
End Sub